package endurance.observer.start;

import endurance.observer.listener.ListenerObserver;
import endurance.observer.Observer;
import endurance.utils.ClassUtils;
import endurance.utils.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @Author: sunlin
 * @Date: 2024-05-17 11:19
 * @Version: 1.0
 * @Description:
 */
@Component
@Slf4j
public class StartService implements ApplicationRunner, ApplicationContextAware {

    @Autowired
    private ClassUtils classUtils;

    @Autowired
    private ListenerObserver listenerObserver;


    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public void run(ApplicationArguments args) throws Exception {

        Map<String, Observer> beansOfType = applicationContext.getBeansOfType(Observer.class);
        for (String key : beansOfType.keySet()) {
            Observer observer = beansOfType.get(key);
            listenerObserver.addObserver(observer);
        }
      /*  List<String> implementationClassNames = classUtils.getImplementationClassNames(Observer.class);
        log.info("当前接口所有的实现类：{}", implementationClassNames);
        for (String className : implementationClassNames) {
            Observer bean = SpringUtils.getBean(className, Observer.class);
            listenerObserver.addObserver(bean);
        }*/
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
